package org.youyun56.gpsserver.apache.tomcat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.youyun56.gpsserver.CacheFactory;
import org.youyun56.gpsserver.Config;

import com.sun.net.httpserver.HttpServer;

public class TomcatServer {
    private static HttpServer httpServer;
    private static ExecutorService executorService;
    private final static Logger log = Logger.getLogger(TomcatServer.class);

    public static void start() throws IOException {
        httpServer = HttpServer.create(new InetSocketAddress(Integer.parseInt(CacheFactory.props.getProperty(Config.HttpPort))), 0);
        httpServer.createContext("/", new GpsHttpHandler());
        executorService = Executors.newCachedThreadPool();
        httpServer.setExecutor(executorService);
        httpServer.start();
    }

    public static void shutDownNow() {
        executorService.shutdownNow();
        httpServer.stop(1);
        log.info("Tomcat server ShutDown..");
    }

    public static void main(String[] args) throws IOException {
        TomcatServer.start();
    }
}
